c
c    To be linked with wp_xi_cf_hod_12.f
c    This module gives the 1-halo and two halo term power spectrum
c    givn N_sat(M) N_cen(M) in a tablar form
c      
      subroutine init_nm(argc2)      
      ! Read model parameters/ In this model
      ! Mcrit_1,alpha1,filename
      parameter (nmass=50,idv=70)
      character*(*) argc2
      character c_or_s*1,nm_fil*80, rline*132, cut_space*132         
      integer nm(0:1)    ! index=0 satellite, 1: central
      real lgm(nmass,0:1),gn(nmass,0:1),lgmx,gnx
      common/wgtpar/nm,lgm,gn
c
c     Read Table for population 2
c
      read (argc2,*) nm_fil
      open (unit=70,file=nm_fil,status='old')
      nmc = 0
      nms = 0
      do while (.true.)
         read (70,'(A)',end=3) rline
         rline = cut_space(rline)
         if (index(rline,'!').ne.1) then
            read (rline,*) c_or_s,lgmx,gnx
            if (lgmx.gt.1e5) lgmx=log10(lgmx) !If entry is lin, covert to log
            if (c_or_s.eq.'c') then
               icen=1 ! Central
            else
               icen=0 ! Satellite
            end if
            nm(icen)=nm(icen)+1
            lgm(nm(icen),icen)=lgmx
            gn(nm(icen),icen)=gnx
         end if
      end do        
 3    close (70)
!     Output Header
      write(6,'(A)')
     $     '! ACF model acf_tbl/From Table'
      write(6,'(A,A,1PE9.2,0PF5.2)')
     $     '!   N(M) file:',nm_fil(1:index(nm_fil,' ')-1)
      end
c
c
      real function galn_halo (rm,isam,icen) 
      ! For the ACF isam=1 and isam =2 gives the same
      ! outputs.
      ! rm: Mass of DMH in solar mass
      ! isam: sample number
      ! icen=0: satellite galaxy, icen=1: central galaxy 
      parameter (nmass=50)
      integer nm(0:1)
      real lgm(nmass,0:1),gn(nmass,0:1),lgmx,gnx
      common/wgtpar/nm,lgm,gn
      lgmx = log10(rm) ! input Log Mh
      if (lgmx.lt.lgm(1,icen)) then
         galn_halo = 0
      else
         imin=1
         imax=nm(icen)
         if (lgmx.gt.lgm(nm(icen),icen)) then
            imin = imax-1
            ff=1.0
         else
            call intra_mono(lgm(1,icen),lgmx,imin,imax,ff)
         end if
         !
         galn_halo =  (1.-ff)*gn(imin,icen)+ff*gn(imax,icen)
         if (icen.eq.0) galn_halo = max(galn_halo,0.) ! Sate
         if (icen.eq.1) galn_halo = min(max(0.,galn_halo),1.0)! cen        
         !
      end if
      return
      end
c
c
      real function wgt_cf(rm,icen) ! Calculate Weight for
                              ! Expected number of pairs 
                              ! between sample 1 and sample 2
      rnmax_subp = 10.0
      prob_max = 0.999 
      rm10 = rm/1e10          ! rm in units of 1e10 solar mass/to match with wgt_samp   

      if (icen.eq.0) then     ! Sat vs Sat, subpoisson behavior 
         rngal = galn_halo(rm,1,0)
         p_sum = 0
         rnpair = 0
         if (rngal.le.rnmax_subp.and.rngal.gt.1e-9) then
            nn=0
            do while (p_sum.lt.prob_max)
               prob = pois (rngal,nn) ! Poisson Distr
               p_sum=p_sum+prob
               rnpair = rnpair+prob*nn*(nn-1)
               nn = nn+1
            end do
         else
            rnpair = rngal*(rngal-1)
         end if
         wgt_cf =  rnpair/rm10/rm10
      else   ! Central vs satellite
         wgt_cf = (galn_halo(rm,1,1)*galn_halo(rm,1,0))/rm10/rm10
      end if   
C      if (rm.gt.1e14.and.rm.le.1e15)
C     $   write (0,'(A,1PE9.1,2(0PF9.2),1PE9.1)') 'DEBUG:M,galn_c,galn_s='
C     $    ,rm,galn_halo(rm,1,1),galn_halo(rm,1,0),wgt_cf
      return
      end
c
      subroutine model_help()
      print '(A)',' ACF Model Specification: acf_tbl.f'
      print '(A)',' N(M)_pars: file'
      print '(A)','  file:: An ASCII file with 3 cols:'
      print '(A)','   List both center (1st col=''c'') and satellite'//
     $     '(1st col=''s'') HODs'
      print '(A)', '  ''c''or ''s'', LogMH [h^{-1}M_sun],N(M)'
      print '(A)', ''
      return
      end
